package com.imoney.framework.hession;

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;

/**
 * 类说明：
 * 
 * Hession 远程调用类
 * 
 * <p>
 * 详细描述： 用来处理Hessian服务的远程调用，借助此类可以很方便的完成方法的远程调用 针对每一个Hessian服务需要实例化一个对象。
 * 
 *
 */
public final class HessianInvoke {

	private String remoteServer;

	private long readTimeout = -1;

	private long connectTimeout = -1;

	private HessianProxyFactory factory;

	/**
	 * 构造方法
	 * 
	 * @param remoteServer
	 *            Hessian 服务地址 如：Http://localhost:8080/biz/hessian/
	 */
	public HessianInvoke(String remoteServer) {
		if (remoteServer != null && !remoteServer.endsWith("/")) {
			this.remoteServer = remoteServer + "/";
		} else {
			this.remoteServer = remoteServer;
		}
		factory = new HessianProxyFactory();
		factory.setConnectTimeout(connectTimeout);
		factory.setReadTimeout(readTimeout);
	}

	public void setReadTimeout(long readTimeout) {
		this.readTimeout = readTimeout;
		factory.setReadTimeout(readTimeout);
	}

	public void setConnectTimeout(long connectTimeout) {
		this.connectTimeout = connectTimeout;
		factory.setConnectTimeout(connectTimeout);
	}

	public void setOverloadEnabled(boolean isOverloadEnabled) {
		factory.setOverloadEnabled(isOverloadEnabled);
	}

	/**
	 * 
	 * 
	 * 方法说明： 获取接口的远程实现对象
	 * 
	 * @param interfaceClazz
	 *            远程调用接口类，服务名称为接口类名去掉第一个字母并小写化
	 * @return 接口类的远程实现
	 */
	public Object getImpl(Class<?> interfaceClazz) {
		return getImpl(interfaceClazz, null);
	}

	/**
	 * 
	 * 
	 * 方法说明： 获取接口的远程实现对象
	 * 
	 * @param interfaceClazz
	 *            远程调用接口类，
	 * @param serviceName
	 *            远程服务名称，如果为空，则服务名为接口类名去掉第一个字母并小写化
	 * @return
	 */
	public Object getImpl(Class<?> interfaceClazz, String serviceName) {

		String url = null;

		if (serviceName != null) {
			url = getUrl(serviceName);
		} else {
			url = getUrl(interfaceClazz.getName().substring(1).toLowerCase());
		}
		try {
			return factory.create(interfaceClazz, url);
		} catch (MalformedURLException e) {
			throw new RuntimeException(
					"invoke remote service throws exception,", e);
		}
	}

	private String getUrl(String serviceName) {
		String url = remoteServer + serviceName;
		return url;
	}

}
